// pages/dingdan/dingdan.js
var defaultColor = "#808080";
var color = "#A01C2C";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showtab: 0,
    textColor1: color,
    textColor2: defaultColor,
    textColor3: defaultColor,
    tabnav: {
      tabnum: 3,
      tabitem: [{
          "id": 0,
          "text": "派单中"
        },
        {
          "id": 1,
          "text": "已接单"
        },
        {
          "id": 2,
          "text": "已完成"
        },
      ]
    },
    refuseList: [],
    doingList: [],
    doneList:[],
    bodyHeight:"700"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRefuseOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  getRefuseOrder: function(e) {
    const that = this;
    //console.log("pai")
    const user = wx.getStorageSync("we_userInfo");
    // var pai = that.data.refuseList.length * 120 + 16;
    // var jie = that.data.doingList.length * 120 + 16;
    // var done = that.data.doneList.length * 120 + 16;
    if (user) {
      wx.request({
        url: getApp().globalData.requestURL + '/api/user/getAssignOrder',
        data: {
          userId: user.user.userId
        },
        method: 'POST',
        header: {
          "content-type": "application/json"
        },
        success: function(res) {
          console.log(res.data.result)
          if (res.data.result != null){
            that.setData({
              refuseList: res.data.result,
              bodyHeight: res.data.result.length * 120 + 110
            })
          }else{
            that.setData({
              refuseList: [],
              bodyHeight: 110
            })
          }
        },
        fail: function(err) {
          console.log(err.errMsg);
          wx.showToast({
            title: err.errMsg,
            icon:"none"
          })
        }
      })
    } else {
      console.log(" 获取缓存失败")
    }

  },
  getDoingOrder: function(e) {
    const that = this;
    console.log("doing")
    const user = wx.getStorageSync("we_userInfo");
    if (user) {
      wx.request({
        url: getApp().globalData.requestURL + '/api/user/getSending',
        data: {
          userId: user.user.userId
        },
        method: 'POST',
        header: {
          "content-type": "application/json"
        },
        success: function(res) {
          console.log(res.data.result)
          if (res.data.result != null){
            that.setData({
              doingList: res.data.result,
              bodyHeight: res.data.result.length * 120 + 110
            })
          }else{
            that.setData({
              doingList: [],
              bodyHeight:110
            })
          }
        },
        fail: function(err) {
          console.log(err.errMsg);
          wx.showToast({
            title: err.errMsg,
            icon:"none"
          })
        }
      })
    } else {
      console.log(" 获取缓存失败")
    }
  },
  getDoneOrder: function(e) {
    console.log("已完成")
    const that = this;
    const user = wx.getStorageSync("we_userInfo");
    if (user) {
      wx.request({
        url: getApp().globalData.requestURL + '/api/user/getOver',
        data: {
          userId: user.user.userId
        },
        method: 'POST',
        header: {
          "content-type": "application/json"
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.result != null){
            that.setData({
              doneList: res.data.result,
              bodyHeight: res.data.result.length * 120 + 110
            })
          }else{
            that.setData({
              doneList: [],
              bodyHeight: 110
            })
          }
        },
        fail: function (err) {
          console.log(err.errMsg);
          wx.showToast({
            title: err.errMsg,
            icon:"none"
          })
        }
      })
    } else {
      console.log(" 获取缓存失败")
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    if (that.data.showtab == 0) {
      that.getRefuseOrder();
    } else if (that.data.showtab == 1){
      that.getDoingOrder();
    }else{
      that.getDoneOrder();
    }
  },
  setTab: function(e) {
    console.log(e)
    var edata = e.currentTarget.dataset;
    var that = this;
    console.log(edata.tabindex)
    //showtab ? (doingList.length * 116 + 16 + 'px') : (refuseList.length * 116 + 16 + 'px'
    if (edata.tabindex == "1") {
      //已接单
      that.setData({
        showtab: edata.tabindex,
        textColor1: defaultColor,
        textColor2: color,
        textColor3: defaultColor,
      })
      that.getDoingOrder()
    }  
    if (edata.tabindex == "0") {
      //派送中
      that.setData({
        showtab: edata.tabindex,
        textColor1: color,
        textColor2: defaultColor,
        textColor3: defaultColor
      })
      that.getRefuseOrder()
    } 
    if (edata.tabindex == "2") {
      //已完成
      that.setData({
        showtab: edata.tabindex,
        textColor1: defaultColor,
        textColor2: defaultColor,
        textColor3: color
      })
      that.getDoneOrder()
    }
  },
  bindchange: function(e) {
    var that = this;
    console.log(e)
    if (e.detail.source == "touch"){
      if (e.detail.current == 1) {
        that.setData({
          showtab: e.detail.current,
          textColor1: defaultColor,
          textColor2: color,
          textColor3: defaultColor
        })
        that.getDoingOrder()
      }
      if (e.detail.current == 0) {
        that.setData({
          showtab: e.detail.current,
          textColor1: color,
          textColor2: defaultColor,
          textColor3: defaultColor
        })
        that.getRefuseOrder()
      }
      if (e.detail.current == 2) {
        //console.log("done")
        //已完成
        //console.log(defaultColor)
        that.setData({
          showtab: e.detail.current,
          textColor1: defaultColor,
          textColor2: defaultColor,
          textColor3: color
        })
        that.getDoneOrder()
      }
    }
  },
  cancel_order: function(e) {
    console.log(e);

  },
  cellClick: function(e) {
    //console.log(e)
    var item = JSON.stringify(e.currentTarget.dataset.item);
    var items = encodeURIComponent(item)
    var index = e.currentTarget.dataset.id;
    if (index == 0) {
      //未接单
      // wx.navigateTo({
      //   url: '../doneOrder/doneOrder?item=' + items,
      // })
    } else {
      //进行中
      wx.navigateTo({
        url: '../dingdanDetails/detail?item=' + items,
      })
    }
  },
  cancel_click: function(e) {
    const that = this;
    console.log(e)
    var orderInfo = e.currentTarget.dataset.item;
    const user = wx.getStorageSync("we_userInfo");
    var openid = user.user.openId;
    var carName = orderInfo.carName;
    var orderId = orderInfo.orderId;
    var agentId = orderInfo.agentId;
    var price = orderInfo.price;

    var data = JSON.stringify({
      openId: user.user.openId,
      carName: carName,
      orderId: orderId,
      agentId: agentId,
      price: price
    })
    console.log(JSON.parse(data));

    if (user) {
      wx.request({
        url: getApp().globalData.requestURL + '/api/order/cancel',
        data: JSON.stringify({
          openId: user.user.openId,
          carName: carName,
          orderId: orderId,
          agentId: agentId,
          price: price
        }),
        method: 'POST',
        header: {
          "content-type": "application/json"
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == "SUCCESS") {
            wx.showModal({
              title: '提示',
              content: res.data.result.msg,
              showCancel: false,
              success(res) {
                console.log("取消成功")
                that.getRefuseOrder()
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.result.msg,
              showCancel: false,
            })
          }
        },
        fail: function(err) {
          console.log(err.errMsg)
        }
      })
    } else {
      console.log(" 获取缓存失败")
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})